﻿// **********************************************
// SharePoint WebPart: UsersADBrowser
// Author: Khristoforov Yuri
// WWW   : http://usersadbrowser.codeplex.com, http://activex.net.ru
// **********************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

using UsersADBrowser.WebParts;

namespace UsersADBrowser.Helpers
{
    public static class FieldTypeHelper
    {
        private const string _defaultImage = "/_layouts/UsersADBrowser/img/person.png";
        public static string presenceHtmlCode = "<span><img border=\"0\" height=\"12\" src=\"/_layouts/images/imnhdr.gif\" align=\"absmiddle\" onload=\"IMNRC('{SIPADDRESS}', this)\" ShowOfflinePawn=\"1\" alt=\"\" style=\"padding-right: 3px; padding-left: 3px; padding-top: 3px; padding-bottom: 3px\" id=\"user_{GUID}_im,type={TYPE}\">";

        public static string FormatEmailField(string val)
        {
            return "<a href=\"mailto:" + val + "\">" + val + "</a>";
        }

        public static string FormatLinkField(string val, string param, string exturl, eUserLinkMode linkMode)
        {
            if (linkMode == eUserLinkMode.DialogWindow)
                return "<a href=\"javascript:UserClick('accountname=" + HttpUtility.UrlEncode(param) + "');\">" + val + "</a>";

            if (linkMode == eUserLinkMode.Page)
                return "<a href=\"/_layouts/UsersADBrowser/UserInfo.aspx?accountname=" + HttpUtility.UrlEncode(param) + "\" target=\"_blank\">" + val + "</a>";

            if (linkMode == eUserLinkMode.ExternalPage)
                return "<a href=\"" + exturl + "?accountname=" + HttpUtility.UrlEncode(param) + "\" target=\"_blank\">" + val + "</a>";

            return "";
        }

        public static string FormatImageField(string val, string imageUrl, int width)
        {
            return "<img src=\"" + ReplaceTmplData(val, imageUrl) + "\" border=\"0\" " + (width > 0 ? "width=\"" + width.ToString() + "\"" : "") + " onerror=\"javascript:DisplayDefaultImage(this);\" />";
        }

        public static string FormatPresenceField(string val, string type)
        {
            string retData = "";

            if (type != "")
                retData = presenceHtmlCode.Replace("{SIPADDRESS}", val).Replace("{GUID}", System.Guid.NewGuid().ToString()).Replace("{TYPE}", type) + "</span>";

            return retData;
        }

        public static string FormatManagerField(string val)
        {
            string[] arrParts = val.Split(',');
            for (int i = 0; i < arrParts.Length; i++)
            {
                if (arrParts[i].Trim().StartsWith("CN="))
                {
                    return arrParts[i].Trim().Substring(3);
                }
            }
            return "";
        }

        private static string ReplaceTmplData(string val, string imageUrl)
        {
            if (imageUrl == "")
                return _defaultImage;

            string[] arrParts = val.Split('@');
            string strUserLogin = arrParts[0];
            string[] arrFullDomainParts = arrParts[1].Split('.');
            string strDomain = arrFullDomainParts[0];
            return imageUrl.Trim().Replace("{LOGIN}", strUserLogin).Replace("{DOMAIN}", strDomain);
        }
    }
}
